// 这里放一些通用的方法
package common

import (
	"errors"
	"httpadmin/go-attribute/application/constant"
	"httpadmin/go-attribute/service/notice/data"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 获取noticeData
func ExtractNoticeData(ctx *gin.Context) (data.IDevice, error) {
	os := getOsType(ctx)
	if os == constant.OsErr {
		return nil, errors.New("无法识别设备类型")
	}
	if os == constant.OsAndroid {
		x := &data.AndroidDeviceInfo{}
		if nil == ctx.Bind(x) {
			return x, nil
		}
	} else if os == constant.OsIOS {
		x := &data.IOSDeviceInfo{}
		if nil == ctx.Bind(x) {
			return x, nil
		}
	}
	return nil, errors.New("错误的设备类型")
}

// 获取 os 类型 安卓还是ios
func getOsType(ctx *gin.Context) int {
	osStr := ctx.Query("os")
	if osStr == "" {
		return constant.OsErr
	}

	os, err := strconv.Atoi(osStr)
	if err != nil {
		return constant.OsErr
	}
	switch os {
	case constant.OsIOS:
		fallthrough
	case constant.OsAndroid:
		return os
	default:
		return constant.OsErr
	}
}
